package com.situ.student.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.student.entity.Banji;
import com.situ.student.entity.PageBean;
import com.situ.student.service.IBanjiService;
import com.situ.student.service.impl.BanjiServiceImpl;

public class BanjiServlet extends HttpServlet {
	private IBanjiService banjiService = new BanjiServiceImpl();

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// post请求乱码问题解决
		req.setCharacterEncoding("utf-8");
		// 增删改查
		String method = req.getParameter("method");
		switch (method) {
		// 查询
		case "list":
			list(req, resp);
			break;
		case "deleteById":
			deleteById(req, resp);
			break;
		case "toupdate":
			toupdate(req, resp);
			break;
		case "update":
			update(req, resp);
			break;
		case "insert":
			insert(req, resp);
			break;
		case "pageList":
			pageBean(req, resp);
			break;
		default:
			break;
		}
	}

	public void pageBean(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		// 接收数据(默认1,3, 然后根据传过来的值再更新)
		String pageNoer = req.getParameter("pageNo");
		String pageSizeer = req.getParameter("pageSize");
		System.out.println("Banji的pageNoer=" + pageNoer);
		int pageNo = 1;
		int pageSize = 3;
		if (pageNoer != null) {
			pageNo = Integer.parseInt(pageNoer);
		}
		if (pageSizeer != null) {
			pageSize = Integer.parseInt(pageSizeer);
		}
		System.out.println(pageNo);
		System.out.println(pageSize);
		// 封装成pagebean，调用service层业务逻辑
		PageBean pageBean = banjiService.getPageBean(pageNo, pageSize);
		System.out.println("班级pageBean=" + pageBean);
		// 转发
		req.setAttribute("pageBean", pageBean);
		req.getRequestDispatcher("/banji_html_list.jsp").forward(req, resp);

	}

	public void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		// 1、得到浏览器传递过来的参数信息
		Integer id = Integer.parseInt(req.getParameter("id"));
		String name = req.getParameter("name");
		Banji banji = new Banji(id, name);
		// 2、调用service处理
		boolean result = banjiService.update(banji);
		System.out.println(result ? "成功" : "失败");
		// 3、转发或者重定向到列表界面
		resp.sendRedirect(req.getContextPath() + "/BanjiServlet?method=list");
	}

	public void toupdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 根据id查找出要修改的banji
		Integer id = Integer.parseInt(req.getParameter("id"));
		// service层逻辑判断
		Banji banji = banjiService.findById(id);
		// 转发到Banji_update.jsp页面
		req.setAttribute("banji", banji);
		req.getRequestDispatcher("/banji_update.jsp").forward(req, resp);
	}

	public void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Integer id = Integer.parseInt(req.getParameter("id"));
		// 2.调用service处理
		boolean result = banjiService.deleteById(id);
		System.out.println(result ? true : false);
		// 重定向到列表界面(删除修改添加使用重定向，查询使用转发)
		resp.sendRedirect(req.getContextPath() + "/BanjiServlet?method=list");
	}

	public void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		// 得到浏览器传递的数据
		Integer banjiid = Integer.parseInt(req.getParameter("id"));
		String name = req.getParameter("name");
		Banji banji = new Banji(banjiid, name);
		System.out.println("banji=" + banji);
		// 调用service处理
		boolean result = banjiService.insert(banji);
		System.out.println(result ? true : false);
		// 重定向刷新界面
		resp.sendRedirect(req.getContextPath() + "/BanjiServlet?method=list");
	}

	public void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<Banji> list = banjiService.list();
		// 将list放到req中
		req.setAttribute("list", list);
		// 转发到classname.jsp页面展示
		req.getRequestDispatcher("/banji_html_list.jsp").forward(req, resp);

	}
}
